package own.stu.bytebuffer.sim.agent;

import net.bytebuddy.agent.builder.AgentBuilder;
import own.stu.bytebuffer.sim.agent.configDynamic.NacosConfigManagerV2;

import java.lang.instrument.Instrumentation;

import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
import static net.bytebuddy.matcher.ElementMatchers.named;

//@Slf4j
public class AgentDes {

    public static String controller_name = "org.springframework.stereotype.Controller";
    public static String rest_controller_name = "org.springframework.web.bind.annotation.RestController";

    public static void premain(String arg, Instrumentation instrumentation) {

        // TODO del
        System.out.println(" ===============  ");
        new NacosConfigManagerV2("127.0.0.1:8848", "frame-config.yaml", "DEFAULT_GROUP");
        try {
            AgentBuilder builder = new AgentBuilder.Default()
                    // 忽略的类
//                .ignore(nameStartsWith("net.bytebuddy").or(nameStartsWith("org.apache")))
                    // 拦截的类
//                .type(isAnnotatedWith(named(controller_name)).or(isAnnotatedWith(named(rest_controller_name))))
                    /*.type(isAnnotatedWith(ElementMatchers.anyOf(
                            ElementMatchers.named(controller_name),
                            ElementMatchers.named(rest_controller_name)
                    )))*/
                    .type(isAnnotatedWith(named(rest_controller_name).or(named(controller_name))))
//                .and(ElementMatchers.<TypeDescription>nameStartsWith("com.own.stu.app.web"))
                    .transform(new AgentTransform())
//                .with(new ByteBuddyListener())
                    ;

            builder.installOn(instrumentation);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
